//
// Created by gyc on 2020/10/16.
//
#include "../../common.h"

class Solution {
public:
    vector<int> sortedSquares(vector<int> &A) {
        auto res = vector<int>(A.size());
        int head = 0, tail = A.size() - 1, index = A.size() - 1;
        while (head <= tail) {
            if (A[head] * A[head] > A[tail] * A[tail]) {
                res[index--] = A[head] * A[head];
                ++head;
            }else {
                res[index--] = A[tail] * A[tail];
                --tail;
            }
        }
        return res;
    }
};

int main() {
    vector<int> vec{-4, -1, 0, 3, 10};

    auto res = Solution().sortedSquares(vec);

    for (int i: res) {
        cout << i << endl;
    }
}